import { loginApi, getInfoApi } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'
export default {
  namespaced: true,
  state: {
    token: getToken(),
    userInfo: {},
    // 按钮权限
    btnList: []
  },
  mutations: {
    setToken(state, token) {
      state.token = token
    },
    setUserInfo(state, userInfo) {
      state.userInfo = userInfo
    },
    removeToken(state) {
      removeToken()
      state.token = null
    },
    removeUserInfo(state) {
      state.userInfo = {}
    },
    // 给按钮权限赋值
    setBtnList(state, data) {
      state.btnList = [...data]
    }
  },
  actions: {
    async  login(ctx, payload) {
      const res = await loginApi(payload)
      console.log(res)
      ctx.commit('setToken', res.data.data)
      setToken(res.data.data)
      // setTime()
    },
    async getUserInfo(ctx, payload) {
      const res = await getInfoApi()
      // const res1 = await getUserDetailById(res.userId)
      console.log(res.data.data.userInfo)
      // const baseResult = { ...res, ...res1 }
      ctx.commit('setUserInfo', res.data.data.userInfo)
      ctx.commit('setBtnList', res.data.data.btnList)
      return res.data.data
    },
    // 退出
    logout(ctx) {
      ctx.commit('removeToken')
      ctx.commit('removeUserInfo')
      // 重置路由实例
      resetRouter()
      // 重置侧边栏信息 讲一个空值给侧边栏,
      // root从根模块开始向下查找
      ctx.commit('permission/getMenuList', [], { root: true })
      // 重置导航块
      // ctx.commit('tagsView/deleTagsView', { root: true })
    }
  }
}

